//
//  AIFactsController.m
//  Mia
//
//  Created by Vill on 2024/2/27.
//

#import "AIFactsController.h"
#import "AIGradientTabView.h"
#import "AIWalletTitleView.h"  // 标题
#import "AIFactsListViewCell.h"
#import "AIFactsListModel.h"   // 模型
#import "AIFactsInterstedViewCell.h"

@interface AIFactsController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UIVisualEffectView *blurView; //模糊
@property (nonatomic, strong) UIButton *closeBtn;
@property (nonatomic, strong) AIGradientTabView *tableView;
@property (nonatomic, strong) AIWalletTitleView *titView;  //标题
@property (nonatomic, strong) AIFactsListModel *factListModel;

@property (nonatomic, strong) NSMutableArray *factsDatas;

@end

@implementation AIFactsController

- (NSMutableArray *)factsDatas{
    if(!_factsDatas){
        _factsDatas = [NSMutableArray array];
    }
    return _factsDatas;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 模糊
    [self.view addSubview:self.blurView];
    [self.blurView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    // 关闭
    [self.view addSubview:self.closeBtn];
    [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(PTW(34));
        make.trailing.mas_equalTo(self.view).mas_offset(-PTW(12.5));
        make.top.mas_equalTo(self.view).mas_offset(KStatusBarHeight + PTW(5));
    }];
    [self.view addSubview:self.tableView];
    
    // 获取客户资料库
    [self getDatabase];
}

#pragma mark ----------------- 获取客户资料库  ------------------------
- (void)getDatabase{
    [SVProgressHUD show];
    [AIManager requestMethod:@"POST" url:api_customGetDatabase parameters:@{} successBlock:^(id  _Nullable result) {
        [SVProgressHUD dismiss];
        self.factListModel = [AIFactsListModel mj_objectWithKeyValues:result];
        [self.factsDatas removeAllObjects];
        if (self.factListModel.factDatas.count > 0){
            for (int i = 0; i<self.factListModel.factDatas.count; i++) {
                AIFactsFactDatasModel *dataModel = self.factListModel.factDatas[i];
                if ([dataModel.topicId strHasValue]){
                    [self.factsDatas addObject:dataModel];
                }
            }
        }
        [self.tableView reloadData];
    } failedBlock:^(NSError * _Nullable error) {
        [SVProgressHUD dismiss];
    } showHUD:NO];
}

#pragma mark ----------------- UITableviewdelegate,UITableViewDataSource  ------------------------

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0){
        AIFactsListViewCell *cell = [AIFactsListViewCell cellWithTableView:tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.section == 0){
            if (indexPath.row == 0){
                // 创建一个日期格式化器，用于解析原始日期字符串
                NSDateFormatter *inputDateFormatter = [[NSDateFormatter alloc] init];
                [inputDateFormatter setDateFormat:@"yyyyMMdd"];
                // 将原始日期字符串转换为 NSDate 对象
                NSDate *birthdayDate = [inputDateFormatter dateFromString:self.factListModel.birthday];
                // 创建另一个日期格式化器，用于格式化目标日期字符串
                NSDateFormatter *outputDateFormatter = [[NSDateFormatter alloc] init];
                [outputDateFormatter setDateFormat:@"yyyy-MM-dd"];
                // 将 NSDate 对象格式化为目标日期字符串
                NSString *formattedBirthdayString = [outputDateFormatter stringFromDate:birthdayDate];
                [cell setTitle:@"Your date of birth" withContent:formattedBirthdayString];
            }else if (indexPath.row == 1){
                //MAN:男；WOMAN:女；UNKNOWN:未知；,可用值:MAN,WOMAN,UNKNOWN
                if ([self.factListModel.sexType isEqualToString:@"MAN"]){
                    [cell setTitle:@"Your Gender" withContent:@"Male"];
                }else if ([self.factListModel.sexType isEqualToString:@"WOMAN"]){
                    [cell setTitle:@"Your Gender" withContent:@"Female"];
                }else{
                    [cell setTitle:@"Your Gender" withContent:@"Unknown"];
                }
            }else{
                AIFactsOtherDatasModel *curOtherDataModel = self.factListModel.otherDatas[indexPath.row - 2];
                [cell setTitle:curOtherDataModel.name withContent:curOtherDataModel.content];
            }
        }
        return cell;
    }else{
        AIFactsInterstedViewCell *cell = [AIFactsInterstedViewCell cellWithTableView:tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.section == 2){
            if ([self.factListModel.factOtherInfo strHasValue]){
                cell.leftImg.image = KImaged(@"ai_facts_otherLogo");
                cell.titleLbl.text = @"Other";
                cell.goodLbl.text = @"";
                cell.contentLbl.text = self.factListModel.factOtherInfo;
            }
        }else{
            AIFactsFactDatasModel *factModel = self.factsDatas[indexPath.row];
            [cell.leftImg sd_setImageWithURL:KUrlImage(factModel.interestTagIcon)];
            cell.titleLbl.text = factModel.interestTagName;
            cell.goodLbl.text = factModel.topicGood ? @"（😁）":@"（😔）";
            cell.contentLbl.text = factModel.topicName;
        }
        return cell;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (section == 0 && self.factListModel.otherDatas.count > 0){
        return 2+self.factListModel.otherDatas.count;
    }else if (section == 1){
        return self.factsDatas.count;
    }else{
        return [self.factListModel.factOtherInfo strHasValue] ? 1:0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0){
        return PTW(90);
    }else{
        return PTW(115);
    }
}

#pragma mark --------------- lazy ---------------
- (UIVisualEffectView *)blurView{
    if(!_blurView){
        UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        _blurView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
        _blurView.alpha = 1;
        _blurView.backgroundColor = [UIColor colorHex:@"#AAAAAA" alpha:0.3];
    }
    return _blurView;
}

- (UIButton *)closeBtn{
    if(!_closeBtn){
        _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeBtn setImage:KImaged(@"ai_main_close") forState:UIControlStateNormal];
        [_closeBtn addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}

-(void)closeAction{
    [self dissMissAction];
    if (self.closeBlock){
        self.closeBlock();
    }
}

- (AIGradientTabView *)tableView{
    if(!_tableView){
        _tableView = [[AIGradientTabView alloc] initWithFrame:CGRectMake(0,KNavigationBarHeight, KScreenWidth, KScreenHeight - KNavigationBarHeight) direction:AIGradualDirectionTop gradualValue:@.2f];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableHeaderView = self.titView;
        self.titView.titleLbl.text = @"Facts";
    }
    return _tableView;
}

- (AIWalletTitleView *)titView{
    if(!_titView){
        _titView = [[AIWalletTitleView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, PTW(80))];
    }
    return _titView;
}


@end
